package com.bovink.util.library;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * 增强版RecyclerView
 *
 * @author bovink
 * @since 2016/8/18
 */
public final class PoweredRecyclerView extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener {
    /**
     * 环境
     */
    private Context context;
    /**
     * 内嵌SwipeRefreshLayout
     */
    private SwipeRefreshLayout swipeRefreshLayout;
    /**
     * 内嵌RecyclerView
     */
    private RecyclerView recyclerView;
    /**
     * 是否到达底部
     */
    private boolean onBottom = false;
    /**
     * 是否正在加载数据
     */
    private boolean onLoading = false;
    /**
     * 下拉刷新接口
     */
    private OnPullDownRefreshListener onPullDownRefreshListener;
    /**
     * 上拉加载接口
     */
    private OnBottomLoadListener onBottomLoadListener;
    /**
     * RecyclerView的adapter
     */
    private PoweredAdapter adapter;
    /**
     * 刷新加载时的等待时间，默认为1s
     */
    private long waitTime = 1000;

    public PoweredRecyclerView(Context context) {
        super(context);
        this.context = context;
        // 解决在预览模式中报错问题
        if (!isInEditMode()) {
            init();
        }
    }

    public PoweredRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        if (!isInEditMode()) {
            init();
        }
    }

    /**
     * 初始化
     */
    private void init() {
        swipeRefreshLayout = this;
        // 添加RecyclerView
        recyclerView = new RecyclerView(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT
        );
        InnerOnScrollListener innerOnScrollListener = new InnerOnScrollListener();
        recyclerView.addOnScrollListener(innerOnScrollListener);
        addView(recyclerView, params);

        // 添加刷新监听
        setOnRefreshListener(this);
        // 设置Spinner颜色策略
        setColorSchemeColors(Color.parseColor("#669900"),Color.parseColor("#0099CC"),Color.parseColor("#FF8800"),Color.parseColor("#9933CC"));

    }

    /**
     * 设置下拉刷新监听
     *
     * @param onPullDownRefreshListener 监听
     */
    public void setOnPullDownRefreshListener(OnPullDownRefreshListener onPullDownRefreshListener) {
        this.onPullDownRefreshListener = onPullDownRefreshListener;
    }

    /**
     * 设置上拉加载监听
     *
     * @param onBottomLoadListener 监听
     */
    public void setOnBottomLoadListener(OnBottomLoadListener onBottomLoadListener) {
        this.onBottomLoadListener = onBottomLoadListener;
    }

    /**
     * 获取内嵌RecyclerView，自定义RecyclerView属性
     * 不要用recyclerView设置adapter
     *
     * @return 内嵌RecyclerView
     */
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    /**
     * 设置适配器
     *
     * @param adapter recyclerView适配器
     */
    public void setAdapter(PoweredAdapter adapter) {
        recyclerView.setAdapter(adapter);
        this.adapter = adapter;
    }

    /**
     * 设置刷新和加载时的等待时间
     * @param waitTime 时间
     */
    public void setWaitTime(long waitTime) {
        this.waitTime = waitTime;
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 如果正在刷新
                if (swipeRefreshLayout.isRefreshing()) {

                    if (onPullDownRefreshListener != null) {
                        onPullDownRefreshListener.onRefresh();
                    }

                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        }, waitTime);
    }

    /**
     * 内部滑动监听
     */
    private class InnerOnScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int visible = recyclerView.getChildCount();
            int total = recyclerView.getLayoutManager().getItemCount();
            int first = linearLayoutManager.findFirstVisibleItemPosition();

            // 还有可以加载的数据
            if (adapter != null && adapter.hasMoreItem) {
                // 到达底部
                if (onBottom) {
                    // 没有加载
                    if (!onLoading) {
                        //TODO 在此处可以让FOOTER播放动画
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                if (onBottomLoadListener != null) {
                                    onBottomLoadListener.onLoad();
                                }
                                onBottom = false;
                                onLoading = false;
                            }
                        }, waitTime);
                        // 开始加载
                        onLoading = true;
                    }

                    // 没有到达底部
                } else {
                    // 判断是否到达底部
                    if (visible + first >= total) {
                        onBottom = true;
                    }
                }
            }
        }
    }

    /**
     * 下拉刷新
     */
    public interface OnPullDownRefreshListener {

        void onRefresh();
    }

    /**
     * 上拉加载
     */
    public interface OnBottomLoadListener {
        void onLoad();
    }

}
